import java.util.Arrays;

class Student {
    public String name;
    public int age;

    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
}


public class Test {

    //去掉前后空格(空白符)
    public static void main(String[] args) {
        String str = "  abcd  abcdef  ";
        String ret = str.trim();
        System.out.println(ret);

    }

    //字符串的截取
    public static void main13(String[] args) {
        String str = "abcdabcdef";
        String ret1 = str.substring(4);
        String ret2 = str.substring(4,6);        //[1,6)
        System.out.println(ret1);
        System.out.println(ret2);
    }

    //字符串拆分
    public static void main12(String[] args) {
        String str1 = "192\\23\\567";         //在语法上，两个\\才算\
        //正则表达式
        String[] ret1 = str1.split("\\\\");          //号需要转义
        for(String x : ret1) {
            System.out.println(x);
        }
    }
    public static void main11(String[] args) {
        String str1 = "192.23.567";
        //正则表达式
        String[] ret1 = str1.split("\\.");          //.号需要转义
        for(String x : ret1) {
            System.out.println(x);
        }
    }

    public static void main10(String[] args) {
        String str1 = "name=zhangsan&age=18";
        //正则表达式
        String[] ret1 = str1.split("=|&");
        for(String x : ret1) {
            System.out.println(x);
        }
    }

    public static void main9(String[] args) {
        String str1 = "name=zhangsan&age=18";
        String[] ret1 = str1.split("&");
        for(String x : ret1) {
            //x = name=zhangsan        age=18
            String[] ret2 = x.split("=");
            for(String x2 : ret2) {
                System.out.println(x2);
            }
            //System.out.println(x);
        }
    }

    public static void main8(String[] args) {
        String str1 = "zhangsan&lisi&wangwu";
        String[] ret1 = str1.split("&");
        for(String x : ret1) {
            System.out.println(x);
        }
        System.out.println("====================");
        String[] ret2 = str1.split("&",2);
        for(String x1 : ret2) {
            System.out.println(x1);
        }
    }


    //替换
    public static void main7(String[] args) {
        String str1 = "Hello";
        String ret = str1.replace('l','p');
        String ret1 = str1.replace("He","kkkk");
        System.out.println(ret);
        System.out.println(ret1);
    }



    //字符串转数组
    public static void main6(String[] args) {
        String str1 = "HELLO";
        char[] str2 = str1.toCharArray();
        for(char ch : str2) {
            System.out.println(ch+ " ");
        }
    }


    //大小写转换
    public static void main5(String[] args) {
        String str1 = "hello";
        String str2 = str1.toUpperCase();
        System.out.println(str1);
        System.out.println(str2);

        String str3 = "WORLD";
        String str4 = str3.toLowerCase();
        System.out.println(str3);
        System.out.println(str4);
    }



    public static void main4(String[] args) {
        //数字转字符串

        String s1 = String.valueOf(1234);
        System.out.println("s1：" + s1);
        String s2 = String.valueOf(12.34);
        System.out.println("s2："+ s2);
        String s3 = String.valueOf(true);
        System.out.println("s3；"+ s3);

        // 如果没有重写 toString() 方法，String.valueOf() 会调用对象的默认 toString() 方法，
        // 将对象转换为字符串形式，
        String s4 = String.valueOf(new Student("zhang san",14));
        System.out.println("s4:"+ s4);

        //字符串转数字
        int data1 = Integer.parseInt("1234");
        double data2 = Double.parseDouble("12.34");
        System.out.println("data1" + data1);
        System.out.println("data2" + data2);
        System.out.println("2222");
    }



    public static void main3(String[] args) {
        String str1 = "hellol";
        char ch = str1.charAt(1);
        System.out.println(ch);


        //找下标
        int index = str1.indexOf('l');
        int index1 = str1.indexOf('l',4);    //从4位置开始找
        System.out.println(index1);
        System.out.println(index);
        int index2 = str1.indexOf("el");
        System.out.println(index2);

        int index3 = str1.lastIndexOf( 'e');  //从后往前数
        System.out.println(index3);


        String str2 = "hello world";
        int index4 = str2.lastIndexOf(' ');
        System.out.println(index4);
    }


    public static void main2(String[] args) {
        String str1 = new String("abcd");
        String str2 = new String("abcd");

        System.out.println(str1 == str2);

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str1.equals(str2));
        System.out.println(str1.compareTo(str2));
        System.out.println(str1.compareToIgnoreCase(str2));   //忽略大小写的情况

        String str3 = "hello";
        String str4 = "hello";
        System.out.println(str3 == str4);
    }


    public static void main1(String[] args) {
        String str1 = new String("abcd");     //定义一个String类对象，值是"abcd"
        String str2 = "hello";               //直接赋值

        //把字符数组转成字符串
        char[] chars = {'w','o','r','l','d'};
        String str3 = new String(chars);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);

        //少见的定义方式
        byte[] bytes = {97,98,99,100};
        String str4 = new String(bytes);
        System.out.println(str4);

    }
}
